<?php
session_start();

include("header.php");
$mydirname = basename( dirname( __FILE__ ) ) ;
	
$myts =& MyTextSanitizer::getInstance();
$module_id = $xoopsModule->getVar('mid');

/* if (is_object($xoopsUser)) {
    $groups = $xoopsUser->getGroups();
} else {
	$groups = XOOPS_GROUP_ANONYMOUS;
}

$gperm_handler =& xoops_gethandler('groupperm');

if (isset($_POST['item_id'])) {
    $perm_itemid = intval($_POST['item_id']);
} else {
    $perm_itemid = 0;
}
//If no access
if (!$gperm_handler->checkRight("resume_submit", $perm_itemid, $groups, $module_id)) {
    redirect_header(XOOPS_URL."/user.php", 3, _NOPERM);
    exit();
} */


/**
 * Funci�n para crear el formulario de registro de empresas
 *
 * @param unknown_type $cid
 */
function registrarempresa($cid)
{
    global $xoopsDB, $xoopsConfig, $xoopsUser, $xoopsTheme, $xoopsLogger, $xoopsModule, $xoopsModuleConfig, $mydirname;

	$token = $GLOBALS['xoopsSecurity']->createToken();
	include_once (XOOPS_ROOT_PATH."/modules/$mydirname/include/functions.php");
	
	include_once (XOOPS_ROOT_PATH."/class/xoopstree.php");
	$mytree = new XoopsTree($xoopsDB->prefix("jobs_res_categories"),"cid","pid");

		
// Detecci�n de errores		
		echo "<script type=\"text/javascript\">
          function verify() {		
		   var n = document.add.login.value;
		   var a = document.add.email.value;
           var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
		    
		   var pass1 = document.add.password.value;
		   var pass2 = document.add.password2.value;		 
                var msg = \""._VALIDAR_EMPRESAS."\\n__________________________________________________\\n\\n\";
                var errors = \"FALSE\";

               if (document.add.login.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDLOGIN."\\n\";
                }
				if (n.length != 0) {   
				if (n.length < \"5\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDNIT."\\n\";
                   }
				}
				if (document.add.password.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDPASSWORD."\\n\";
                }
				
				if (pass1.length != 0) {   
				if (pass1.length < \"4\") {
                        errors = \"TRUE\";
                        msg += \""._PASS_LONGER."\\n\";
                   }
				}
				
				if (pass1 != pass2) {
                        errors = \"TRUE\";
                        msg += \""._PASS_ERROR."\\n\";
                }
			    if (document.add.empresa.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDEMPRESA."\\n\";
                }          				
                if (document.add.telefono.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDTELEFONO."\\n\";
                }
				if (document.add.direccion.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDDIRECCION."\\n\";
                }
				if (document.add.ciudad.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDCIUDADEMP."\\n\";
                }
				if (document.add.sector.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDSECTOR."\\n\";
                }
				if (document.add.actividad.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDACT."\\n\";
                }
				if (document.add.representante.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDREP."\\n\";
                }
				if (document.add.cargo.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDCARGOREP."\\n\";
                }
				if (document.add.email.value == \"\") {
                        errors = \"TRUE\";
                        msg += \""._VALIDREPEMAIL."\\n\";
                }
				if (a.length != 0) {     
                  if (!filter.test(a)) {
			           errors = \"TRUE\";
                       msg += \""._EMAIL_ERROR."\\n\";
			     }
			 }    
			 if (document.add.acuerdo.checked == false) {
                        errors = \"TRUE\";
                        msg += \""._VALIDACUERDO."\\n\";
                }
				
                if (errors == \"TRUE\") {
                        msg += \"__________________________________________________\\n\\n"._VALIDAR_EMPRESAS2."\\n\";
                        alert(msg);
                        return false;
                }
          }
          </script>";

	  echo "<script type=\"text/javascript\">
	 function numeros(e) {
    tecla = (document.all) ? e.keyCode : e.which; 
    if (tecla==8) return true; 
    patron =/\d/; 
    te = String.fromCharCode(tecla); 
    return patron.test(te);
    } 
      </script>"; 
	  
	    echo "<script type=\"text/javascript\">
	function letras(e) {
    tecla = (document.all) ? e.keyCode : e.which; 
    if (tecla==8) return true; 
    patron =/[A-Za-z��\s]/; 
    te = String.fromCharCode(tecla); 
    return patron.test(te);
    } 
      </script>"; 
 
		list($numrows) = $xoopsDB->fetchRow($xoopsDB->query("select cid, title, affprice from ".$xoopsDB->prefix("jobs_res_categories").""));
    
		if ($numrows>0) {
			
			echo "<table width='100%' border='0' cellspacing='1' cellpadding='8'><tr class='bg4'><td valign='top'>\n";
		
			echo "<b><center>"._FORM_REGISTRO."</center></b><br /><br /><center>"._INFOR2."</center><br /><br />";

			echo "<form method=\"post\" action=\"empresas.php\" enctype=\"multipart/form-data\" name=\"add\" onsubmit=\"return verify();\">
			
			   <b>"._DATOS_LOGIN."
			    <table width='100%' class='outer' cellspacing='1'><tr>
				<td width='35%' class='odd'>"._LOGIN_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"login\" size=\"25\" maxlength=\"12\" onKeypress=\"return numeros(event);\"/></td>
				</tr><tr>
				<td width='35%' class='even'>"._PASSWORD_EMPRESA2." </td><td class='even'><input type=\"password\" name=\"password\" size=\"25\" maxlength=\"12\" onKeypress=\"return numeros(event);\"/>
				&nbsp;"._PASSWORD_AGAIN."&nbsp;<input type=\"password\" name=\"password2\" size=\"25\" maxlength=\"12\" onKeypress=\"return numeros(event);\"/></td>
				</tr></table><br /><br>
				
			    "._INFO_SOLICITANTE."
			    <table width='100%' class='outer' cellspacing='1'><tr>
				<td width='35%' class='odd'>"._NOMBRE_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"empresa\" size=\"50\" maxlength=\"100\" /></td>
				</tr><tr>
				<td width='35%' class='even'>"._TELEFONO_EMPRESA." </td><td class='even'><input type=\"text\" name=\"indicativo\" size=\"5\" maxlength=\"5\" onKeypress=\"return numeros(event);\"/>
				- &nbsp;<input type=\"text\" name=\"telefono\" size=\"20\" maxlength=\"20\" onKeypress=\"return numeros(event);\"/>&nbsp;"._EXT."&nbsp;<input type=\"text\" name=\"ext\" size=\"5\" maxlength=\"5\" onKeypress=\"return numeros(event);\"/></td>
				</tr><tr>
				<td width='35%' class='odd'>"._FAX_EMPRESA._OPCIONAL." </td><td class='odd'><input type=\"text\" name=\"indfax\" size=\"5\" maxlength=\"5\" onKeypress=\"return numeros(event);\"/>
				- &nbsp;<input type=\"text\" name=\"fax\" size=\"20\" maxlength=\"20\" onKeypress=\"return numeros(event);\"/></td>
				</tr><tr>
				<td width='35%' class='even'>"._CIUDAD_EMPRESA." </td><td class='even'><input type=\"text\" name=\"ciudad\" size=\"50\" maxlength=\"50\" /></td>
				</tr><tr>
				<td width='35%' class='odd'>"._DIRECCION_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"direccion\" size=\"50\" maxlength=\"50\" /></td>
				</tr><tr>
				<td width='35%' class='even'>"._SECTOR_EMPRESA." </td><td class='even'>
				<select name=\"sector\"><option selected></option>";
				
				$sectorlist = array(_SECTOR1,
				                    _SECTOR7,
				                    _SECTOR2,
				                    _SECTOR3,
				                    _SECTOR4,
				                    _SECTOR5,
									_SECTOR6,
				);
				
				foreach ($sectorlist as $value){
				    echo "<option value=\"{$value}\">{$value}</option>";
				}
				
				echo "</select></td>
				</tr><tr>
				<td width='35%' class='odd'>"._ACTIVIDAD_EMPRESA." </td><td class='odd'><input type=\"text\" name=\"actividad\" size=\"50\" maxlength=\"100\" /></td>
				</tr></table><br /><br>
				
			    <table width='100%' class='outer' cellspacing='1'>
				"._INFO_RESPONSABLE." <tr>
				<td width='35%' class='even'>"._REPRESENTANTE_EMPRESA." </td><td class='even'><input type=\"text\" name=\"representante\" size=\"50\" maxlength=\"50\" onKeypress=\"return letras(event);\"/></td>
				</tr><tr>
				<td width='35%' class='odd'>"._CARGO_REPRESENTANTE." </td><td class='odd'><input type=\"text\" name=\"cargo\" size=\"50\" maxlength=\"100\" /></td>
				</tr>";

				if($xoopsUser) {
				$iddd =$xoopsUser->getVar("uid", "E");
				$idd =$xoopsUser->getVar("name", "E");// Real name
				/* $idde =$xoopsUser->getVar("email", "E"); */
				$iddn =$xoopsUser->getVar("uname", "E");// user name
			}
					
				echo "<tr>
				<td width='35%' class='even'>"._CORREO_EMPRESA." </td><td class='even'><input type=\"text\" name=\"email\" size=\"50\" maxlength=\"50\" /></td>
				</tr><tr></b>";
			
				$time = time();

				echo "</table><br /><br>
				<center><div align=\"justify\" style=\"OVERFLOW-Y: scroll; OVERFLOW-X: hidden; WIDTH: 450px; HEIGHT: 110px;\">"._CONDICIONES."</div><br><br>
				<input type=\"checkbox\" name=\"acuerdo\"/>&nbsp;"._ACUERDO."</center><br><br>
				<input type=\"hidden\" name=\"usid\" value=\"$iddd\" />";
				/* <input type=\"hidden\" name=\"email\" value=\"$idde\" /> */
				echo "<input type=\"hidden\" name=\"submitter\" value=\"$iddn\" />
				<input type=\"hidden\" name=\"op\" value=\"registrarempresaOk\" />";
				echo "<input type=\"hidden\" name=\"token\" value=\"$token\" />";
			    echo "<input type=\"hidden\" name=\"lid\" value=\"0\" />
				<input type=\"hidden\" name=\"fechareg\" value=\"$time\" />
				<center><input type=\"submit\" value=\""._JOBS_VALIDATE."\" />
				<input name=\"btncancelar\" type=\"button\" id=\"btncancelar\" onClick=\"self.location.href='bienvenida.php'\"  value=\""._CANCELAR."\"></center>";
			echo "</form>";
			echo "</td></tr></table>";
			echo "<br><center>"._ADVERTENCIA."<br><br></center>";
	   	}
	}

/**
 * Enter description here...
 *
 * @param unknown_type $cid
 * @param unknown_type $login
 * @param unknown_type $password
 * @param unknown_type $empresa
 * @param unknown_type $telefono
 * @param unknown_type $direccion
 * @param unknown_type $ciudad
 * @param unknown_type $sector
 * @param unknown_type $actividad
 * @param unknown_type $representante
 * @param unknown_type $cargo
 * @param unknown_type $email
 * @param unknown_type $fechareg
 */
function registrarempresaOk($login, $password, $empresa, $telefono, $indicativo, $ext, $direccion, $fax, $indfax, $ciudad, $sector, $actividad, $representante, $cargo, $email, $fechareg)
{

	global $xoopsDB, $xoopsConfig, $xoopsModuleConfig, $destination, $myts, $xoopsLogger, $mydirname;

		if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['token'])) {
		   redirect_header(XOOPS_URL."/modules/$mydirname/bienvenida.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		}
	
	include(XOOPS_ROOT_PATH."/modules/$mydirname/include/functions.php");

	$login = $myts->makeTboxData4Save($login);
	$password = $myts->makeTboxData4Save($password);
	$empresa = $myts->makeTboxData4Save($empresa);
	$telefono = $myts->makeTboxData4Save($telefono);
	$indicativo = $myts->makeTboxData4Save($indicativo);
	$ext = $myts->makeTboxData4Save($ext);
	$direccion = $myts->makeTboxData4Save($direccion);
	$fax = $myts->makeTboxData4Save($fax);
	$indfax = $myts->makeTboxData4Save($indfax);
	$ciudad = $myts->makeTboxData4Save($ciudad);
	$sector = $myts->makeTboxData4Save($sector);
	$actividad = $myts->makeTboxData4Save($actividad);
	$representante = $myts->makeTboxData4Save($representante);
	$cargo = $myts->makeTboxData4Save($cargo);
	$email = $myts->makeTboxData4Save($email);
	
//	$filename = '';

// comprueba si el NIT ingresado en el registro ya fue regsitrado por una empresa
   list($nitexists) = $xoopsDB->fetchRow($xoopsDB->query("select login FROM ".$xoopsDB->prefix("jobs_empresas")." WHERE login=$login"));
   if ($nitexists){
   redirect_header("registro_empresas.php",3,_NITEXISTS); //redirect_header($url, $time = 3, $message = '', $addredirect = true)  
   exit();
   }


//Inserta en la tabla "jobs_empresas" los valores de las variables
	$xoopsDB->query("INSERT INTO ".$xoopsDB->prefix("jobs_empresas")." values ('$login', '$password', '$empresa', '$telefono', '$indicativo', '$ext', '$direccion', '$fax', '$indfax', '$ciudad', '$sector', '$actividad', '$representante', '$cargo', '$email', '$fechareg')");
//Inserta en la tabla "jobs_password" el nombre de usuario y el password con el que se loguear� la empresa	
//	$xoopsDB->query("INSERT INTO ".$xoopsDB->prefix("jobs_password")." values ('$login', '$password', '3')");
	
	$_SESSION['datos_admin']['estado']= "autenticado";
	$_SESSION['datos_admin']['tipo']= 3;
	$_SESSION['datos_admin']['user']= $login;
	redirect_header("principalempresas.php",3,_REGISTROOK); //redirect_header($url, $time = 3, $message = '', $addredirect = true)  
    exit();
}	




#######################################################
foreach ($_POST as $k => $v) {
	${$k} = $v;
}


if(!isset($_POST['cid']) && isset($_GET['cid']) ) {
	$cid = $_GET['cid'] ;
}

if(!isset($_POST['op']) && isset($_GET['op']) ) {
	$op = $_GET['op'] ;
}

if (!isset($op)) {
	$op = '';
}

switch($op) {	
	case "registrarempresaOk":
   	registrarempresaOk($login, $password, $empresa, $telefono, $indicativo, $ext, $direccion, $fax, $indfax, $ciudad, $sector, $actividad, $representante, $cargo, $email, $fechareg);
   	break;

 

	default:
	include(XOOPS_ROOT_PATH."/header.php");
	registrarempresa($cid);
	
	echo "<br /><br />
<div align=\"center\">" . _INFO_PAG . "</div>
<br /><br />";
	
	include(XOOPS_ROOT_PATH."/footer.php");
	break;
}

?>
